﻿$(document).ready(function () {
    // Smart Wizard 	
    //    $('#wizard').smartWizard();

    //    function onFinishCallback() {
    //        $('#wizard').smartWizard('showMessage', 'Finish Clicked');
    //    }

    $("#txtBox_adultNum").change(function () {
        setCustomerDetailsRow();
    });

    $("#txtBox_childNum").change(function () {
        setCustomerDetailsRow();
    });        
});


function setCustomerDetailsRow() {
    var childNum = $("#txtBox_childNum")[0].value;
    var adultNum = $("#txtBox_adultNum")[0].value;
    var customerTotal = $("#txtCustomerTotal")[0];
    var trArr = $("#dk_customerListBody tr");
        var price = $(".txtPriceAdult")[0].value;
//    var price = 0;
    if (isNaN(childNum) && isNaN(adultNum)) {
        customerTotal.value = 0;
    }
    if (isNaN(childNum) && !isNaN(adultNum)) {
        customerTotal.value = parseInt(adultNum);
    }
    if (!isNaN(childNum) && !isNaN(adultNum)) {
        customerTotal.value = parseInt(childNum) + parseInt(adultNum);
    }

    if (parseInt(customerTotal.value) < 0) {
        customerTotal.value = 0;
    }

    if (customerTotal.value == 0) {
        for (var i = 1; i < trArr.length; i++) {
            //                var ele = $("#dk_customerListBody tr")[i];
            //                $("body").children("a").eq(0).remove();
            $("#dk_customerListBody").children("tr").eq(i).remove();
        }

    }
    else {
        if (customerTotal.value < trArr.length - 1) {
            for (var i = trArr.length; i >customerTotal.value; i--) {
                $("#dk_customerListBody").children("tr").eq(i).remove();
            }
        }
        else {
            for (var i = trArr.length; i <= customerTotal.value; i++) {
                var customerTypeArr = $('.txtCustomerType');
                var str = "";
                for (var j = 0; j < customerTypeArr.length; j++) {
                    str = str + " <option label='" + customerTypeArr[j].value + "' >" + customerTypeArr[j].value + "</option>";
                }
                var ele = "<tr class='row_even customerDetails'>"
                            + "<td>"
                            + (parseInt(i)).toString()
                            + "</td>"
                            + "<td>"
                            + "   <input type='text' name='txtHoTen' class='txtHoTen' />"
                            + "</td>"
                            + "<td>"
                            + "   <input type='text' name='txtNgaySinh' class='txtNgaySinh' />"
                            + "</td>"
                            + "<td>"
                            + "   <input type='text' name='txtDiaChi' class='txtDiaChi' />"
                            + "</td>"
                            + "<td>"
                            + "   <input type='text' name='txtPassport' class='txtPassport' />"
                            + "</td>"
                            + "<td>"
                            + "   <select name='txtGioiTinh' class='cboGioiTinh' tabindex='-1'>"
                            + "      <option label='Nữ' value='0'>Nữ</option>"
                            + "      <option label='Nam' value='1'>Nam</option>"
                            + "   </select>"
                            + "</td>"
                            + "<td>"
                            + "    <select name='cboDoTuoi' class='cboDoTuoi' tabindex='-1' onchange='setPrice(" + parseInt(i) + ");'>"
                            + "       <option label='Người lớn' value='0'>Người lớn</option>"
                            + "       <option label='Trẻ em' value='1'>Trẻ em</option>"
                            + "   </select>"
                            + "</td>"
                            + "<td>"
                            + "   <select name='cboLoaiPhong' class='cboLoaiPhong' tabindex='-1'>"
                            + "        <option label='Phòng đơn' value='0'>Phòng đơn</option>"
                            + "        <option label='Phòng đôi' value='1'>Phòng đôi</option>"
                            + "   </select>"
                            + "</td>"
                            + "<td>"
                            + "   <select name='cboLoaiKhach' class='cboLoaiKhach' tabindex='-1' onchange='setPrice(" + parseInt(i) + ");'>"
                            + str
                            + "   </select>"
                            + "</td>"
                            + "<td class='price'>"
                            + price.toString() + "<input type='hidden' name='txtSubTotal' onchange='setPrice(" + parseInt(i) + ");' value='" + price.toString() + "' />"
                            + "</td>"
                            + "</tr>";
                $("#dk_customerListBody").append(ele);
            }
        }
    }
}

function setPrice(id) {
    var doTuoi = $('.cboDoTuoi')[id - 1].selectedIndex;
    var loaiKhach = $('.cboLoaiKhach')[id - 1].selectedIndex;
    if (doTuoi == 0) { 
        $('.price')[id-1].innerHTML = $(".txtPriceAdult")[loaiKhach].value;
        return;
    }

    if (doTuoi == 1) {
        $('.price')[id - 1].innerHTML = $(".txtPriceChild")[loaiKhach].value;
        return;
    }

}

function setInit() {

    SetPriceForDefault('4918000', '3114000', '1964000', '272000', '2618000', '1964000', '272000', '0', '0', '0', '1200000', '0', '0', '0', '2300000', '2300000');

}

if (window.attachEvent) { window.attachEvent("onload", setInit); }
else {
    setInit();
}

//addRow('customerListBody');

